import { List, Toast, Button, WingBlank } from 'antd-mobile';
import { Component } from 'react'
import { connect } from 'dva'
import router from 'umi/router';
import StandardForm from '@/components/standardForm'
@connect(({ auth, loading }) => {
  return {
    auth,
  }
})
class Create extends Component {
  constructor(props) {
    super(props)
    this.changePwd = this.changePwd.bind(this)
  }
  instantForm = null
  changePwd() {
    const { dispatch } = this.props
    this.instantForm.validateFields((err, values) => {
      if(err) return
      if(values.newPwd !== values.newPwdCopy) {
        Toast.fail('新密码与确认密码不一致。')
        return
      }
      dispatch({
        type: 'manager/changePwd',
        payload: values
      }).then(res => {
        Toast.success(res.msg)
        setTimeout(() => {
          router.go(-1)
        }, 1500)
      })
    })

  }
  formItems = [
    {
      name: 'originPwd',
      label: '原密码',
      placeholder: '请输入原密码',
      layoutOption: {
        type: 'password'
      },
      options: {
        rules: [{required: true}]
      }
    },
    {
      name: 'newPwd',
      label: '新密码',
      placeholder: '请输入新密码',
      layoutOption: {
        type: 'password'
      },
      options: {
        rules: [{required: true}]
      }
    },
    {
      name: 'newPwdCopy',
      label: '确认密码',
      placeholder: '请输入确认密码',
      layoutOption: {
        type: 'password'
      },
      options: {
        rules: [{required: true}]
      }
    }
  ]
  render() {
    return (
      <WingBlank style={{paddingTop: '200px'}} >
        <List>
          <StandardForm 
            wrappedComponentRef={el => this.instantForm = el}
            formItems={this.formItems}
          />
          {/* <InputItem>原密码:</InputItem>
          <InputItem>新密码:</InputItem>
          <InputItem>确认密码:</InputItem> */}
        </List>
        <Button style={{margin: '15px 0'}} type="primary" onClick={this.changePwd}>提交</Button>
      </WingBlank>
    )
  }
}
export default Create